package software;

import java.io.*;
import java.util.ArrayList;

public class Judegement {
	//待批改文件(只有答案)、练习文件(只有算式)、批改后文件(存储批改结果)
	void Judege(File CheckingFile,File PracticeFile,File CorrectedFile){
		try {
			Practice p = new Practice();
			ArrayList<Integer> results = p.readCSVPractice(CheckingFile);
			Exercise anExercise = new Exercise();
			
			anExercise = anExercise.readCSVBinaryExercise(PracticeFile);
			Integer[] answers = new Integer[anExercise.size()];
			for(int i=0;i<anExercise.size();++i) {
				answers[i] = anExercise.get(i).getResult();
			}
		
		
			if(!CorrectedFile.exists()) {
				CorrectedFile.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(CorrectedFile.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fileWriter);
			
			int sum = anExercise.size();
			int right = 0;
			bw.write(PracticeFile.getName()+"的批改结果:\n");
			for(int i=0;i<sum;++i) {
				String str = null;
				if(i<results.size()&&results.get(i).equals(answers[i])) {
					str="正确";
					right++;
				}
				else str="错误";
				String str2 = null;
				if(i<results.size()) {
					if(results.get(i)==-1000)str2="非法结果";
					else str2 = results.get(i).toString();
				}else str2 = "未给结果";
				bw.write(""+(i+1) +") :" + "算式：" + String.format("%-10s", anExercise.get(i).asString()) + 
						" 给出的结果：" + str2 + "   正确结果：" + String.format("%-4s", answers[i]) + "   评测结果： "+str+"\n");
				
			}
			bw.write("算式个数："+sum+"\n");
			bw.write("正确个数："+right+"\n");
			bw.write("错误个数：" + (sum-right)+"\n");
			if(sum!=0)bw.write("得分：" + 1.0*right/sum*100+"\n");
			System.out.println(PracticeFile.getName()+"已批改完毕，并存入到:\n\t"+
					CorrectedFile.getAbsolutePath());
			bw.close();
			fileWriter.close();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
}
